package com.blogs.service;

import android.content.Context;

import com.blogs.entity.AppConfig;
import com.blogs.entity.MsgEntity;
import com.blogs.tools.GetTextFromWeb;
import com.blogs.tools.TbContent;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class OffLineGetBlogDetail {
	private TbContent db;
	private String blog_url = AppConfig.GETBLOGDETAIL_URL;
	private String url = "";

	public OffLineGetBlogDetail(Context context, String id) {
		// TODO Auto-generated constructor stub
		url += blog_url.replace("{0}", id);
		db = new TbContent(context, id.trim());
	}

	public boolean loadData() {
		String result = GetTextFromWeb.Get(this.url);
		if(result.equals(""))
		{
			if (db != null) {
				db.DbClose();
			}
			return false;
		}
		GsonBuilder builder = new GsonBuilder();
		Gson gson = builder.create();
		MsgEntity<Object> msg = null;
		try {
			msg = gson.fromJson(result, MsgEntity.class);
		} catch (Exception e) {
			if (db != null) {
				db.DbClose();
			}
			return false;
		}
		if (msg.op.equals("")) {
			if (db != null) {
				db.DbClose();
			}
			return false;
		}
		String con = (String) msg.data;
		if (con == null) {
			con = "";
		}
		if (db != null) {
			try {
				db.Insert(con);
			} catch (Exception e) {

			}
			db.DbClose();
		}
		return true;
	}
}
